﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类:DALGoodsType
    /// </summary>
    public partial class DALGoodsType
    {
        public DALGoodsType()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int typeId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbGoodsType");
            strSql.Append(" where typeId=@typeId");
            SqlParameter[] parameters = {
					new SqlParameter("@typeId", SqlDbType.Int,4)
			};
            parameters[0].Value = typeId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.tbGoodsType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbGoodsType(");
            strSql.Append("parentId,typeName,typeOrder,typedepth,visible,typeLink,typeContent,typePic,ishead,typeKey)");
            strSql.Append(" values (");
            strSql.Append("@parentId,@typeName,@typeOrder,@typedepth,@visible,@typeLink,@typeContent,@typePic,@ishead,@typeKey)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@parentId", SqlDbType.Int,4),
					new SqlParameter("@typeName", SqlDbType.NVarChar,150),
					new SqlParameter("@typeOrder", SqlDbType.Int,4),
					new SqlParameter("@typedepth", SqlDbType.Int,4),
					new SqlParameter("@visible", SqlDbType.Bit,1),
					new SqlParameter("@typeLink", SqlDbType.NVarChar,150),
					new SqlParameter("@typeContent", SqlDbType.NVarChar,2000),
					new SqlParameter("@typePic", SqlDbType.NVarChar,150),
					new SqlParameter("@ishead", SqlDbType.Bit,1),
					new SqlParameter("@typeKey", SqlDbType.NVarChar,250)};
            parameters[0].Value = model.parentId;
            parameters[1].Value = model.typeName;
            parameters[2].Value = model.typeOrder;
            parameters[3].Value = model.typedepth;
            parameters[4].Value = model.visible;
            parameters[5].Value = model.typeLink;
            parameters[6].Value = model.typeContent;
            parameters[7].Value = model.typePic;
            parameters[8].Value = model.ishead;
            parameters[9].Value = model.typeKey;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gift.Model.tbGoodsType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbGoodsType set ");
            strSql.Append("parentId=@parentId,");
            strSql.Append("typeName=@typeName,");
            strSql.Append("typeOrder=@typeOrder,");
            strSql.Append("typedepth=@typedepth,");
            strSql.Append("visible=@visible,");
            strSql.Append("typeLink=@typeLink,");
            strSql.Append("typeContent=@typeContent,");
            strSql.Append("typePic=@typePic,");
            strSql.Append("ishead=@ishead,");
            strSql.Append("typeKey=@typeKey");
            strSql.Append(" where typeId=@typeId");
            SqlParameter[] parameters = {
					new SqlParameter("@parentId", SqlDbType.Int,4),
					new SqlParameter("@typeName", SqlDbType.NVarChar,150),
					new SqlParameter("@typeOrder", SqlDbType.Int,4),
					new SqlParameter("@typedepth", SqlDbType.Int,4),
					new SqlParameter("@visible", SqlDbType.Bit,1),
					new SqlParameter("@typeLink", SqlDbType.NVarChar,150),
					new SqlParameter("@typeContent", SqlDbType.NVarChar,2000),
					new SqlParameter("@typePic", SqlDbType.NVarChar,150),
					new SqlParameter("@ishead", SqlDbType.Bit,1),
					new SqlParameter("@typeKey", SqlDbType.NVarChar,250),
					new SqlParameter("@typeId", SqlDbType.Int,4)};
            parameters[0].Value = model.parentId;
            parameters[1].Value = model.typeName;
            parameters[2].Value = model.typeOrder;
            parameters[3].Value = model.typedepth;
            parameters[4].Value = model.visible;
            parameters[5].Value = model.typeLink;
            parameters[6].Value = model.typeContent;
            parameters[7].Value = model.typePic;
            parameters[8].Value = model.ishead;
            parameters[9].Value = model.typeKey;
            parameters[10].Value = model.typeId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int typeId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbGoodsType ");
            strSql.Append(" where typeId=@typeId");
            SqlParameter[] parameters = {
					new SqlParameter("@typeId", SqlDbType.Int,4)
			};
            parameters[0].Value = typeId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string typeIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbGoodsType ");
            strSql.Append(" where typeId in (" + typeIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbGoodsType GetModel(int? typeId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 typeId,parentId,typeName,typeOrder,typedepth,visible,typeLink,typeContent,typePic,ishead,typeKey from tbGoodsType ");
            strSql.Append(" where typeId=@typeId");
            SqlParameter[] parameters = {
					new SqlParameter("@typeId", SqlDbType.Int,4)
			};
            parameters[0].Value = typeId;

            Gift.Model.tbGoodsType model = new Gift.Model.tbGoodsType();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["typeId"] != null && ds.Tables[0].Rows[0]["typeId"].ToString() != "")
                {
                    model.typeId = int.Parse(ds.Tables[0].Rows[0]["typeId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["parentId"] != null && ds.Tables[0].Rows[0]["parentId"].ToString() != "")
                {
                    model.parentId = int.Parse(ds.Tables[0].Rows[0]["parentId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["typeName"] != null && ds.Tables[0].Rows[0]["typeName"].ToString() != "")
                {
                    model.typeName = ds.Tables[0].Rows[0]["typeName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["typeOrder"] != null && ds.Tables[0].Rows[0]["typeOrder"].ToString() != "")
                {
                    model.typeOrder = int.Parse(ds.Tables[0].Rows[0]["typeOrder"].ToString());
                }
                if (ds.Tables[0].Rows[0]["typedepth"] != null && ds.Tables[0].Rows[0]["typedepth"].ToString() != "")
                {
                    model.typedepth = int.Parse(ds.Tables[0].Rows[0]["typedepth"].ToString());
                }
                if (ds.Tables[0].Rows[0]["visible"] != null && ds.Tables[0].Rows[0]["visible"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["visible"].ToString() == "1") || (ds.Tables[0].Rows[0]["visible"].ToString().ToLower() == "true"))
                    {
                        model.visible = true;
                    }
                    else
                    {
                        model.visible = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["typeLink"] != null && ds.Tables[0].Rows[0]["typeLink"].ToString() != "")
                {
                    model.typeLink = ds.Tables[0].Rows[0]["typeLink"].ToString();
                }
                if (ds.Tables[0].Rows[0]["typeContent"] != null && ds.Tables[0].Rows[0]["typeContent"].ToString() != "")
                {
                    model.typeContent = ds.Tables[0].Rows[0]["typeContent"].ToString();
                }
                if (ds.Tables[0].Rows[0]["typePic"] != null && ds.Tables[0].Rows[0]["typePic"].ToString() != "")
                {
                    model.typePic = ds.Tables[0].Rows[0]["typePic"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ishead"] != null && ds.Tables[0].Rows[0]["ishead"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["ishead"].ToString() == "1") || (ds.Tables[0].Rows[0]["ishead"].ToString().ToLower() == "true"))
                    {
                        model.ishead = true;
                    }
                    else
                    {
                        model.ishead = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["typeKey"] != null && ds.Tables[0].Rows[0]["typeKey"].ToString() != "")
                {
                    model.typeKey = ds.Tables[0].Rows[0]["typeKey"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select typeId,parentId,typeName,typeOrder,typedepth,visible,typeLink,typeContent,case isnull(typePic,'') when '' then 'default.jpg' else typePic end as typePic,ishead,typeKey,(select typeName from tbGoodsType where typeId = tbGoodsType.parentId) as parentName, ");
            strSql.Append(" [typeName] + (select '[' + convert(nvarchar(10),count(1)) + ']' from tbGoodsType g where g.parentId = tbGoodsType.typeId ) as typeDec  ");
            strSql.Append(" FROM tbGoodsType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListForHavGoods()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select gt.typeId,gt.parentId,gt.typeName,gt.typeOrder,gt.typedepth,gt.visible,gt.typeLink,gt.typeContent,ishead,typeKey,(select typeName from tbGoodsType where typeId = gt.parentId) as parentName ");
            strSql.Append(" FROM tbGoodsType gt  ");

            strSql.Append(" where isnull(gt.visible,0) =1 and ((typeDepth=1 and typeId in (select parentId from tbGoodsType where typeId in (select typeId from tbGoods where isnull(visible,0)=1)))  ");
            strSql.Append(" or (typeDepth = 2 and typeId in (select typeId from tbGoods where isnull(visible,0)=1))) ");

    
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetMainGoodsTypeList()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select typeId,parentId,typeName,typeOrder,typedepth,visible,typeLink,typeContent,case isnull(typePic,'') when '' then 'default.jpg' else typePic end as typePic,ishead,typeKey, ");
            strSql.Append(" [typeName] + (select '[' + convert(nvarchar(10),count(1)) + ']' from tbGoodsType g where g.parentId = tbGoodsType.typeId ) as typeDec  ");
            strSql.Append(" FROM tbGoodsType ");
            strSql.Append(" where isnull(visible,0)=1 and typedepth=0 and typeName !='限时抢购'");

            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetMainMenuList(string sMenuName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select typeId,parentId,typeName,typeOrder,typedepth,visible,typeLink,typeContent,case isnull(typePic,'') when '' then 'default.jpg' else typePic end as typePic,ishead,typeKey, ");
            strSql.Append(" [typeName] + (select '[' + convert(nvarchar(10),count(1)) + ']' from tbGoodsType g where g.parentId = tbGoodsType.typeId ) as typeDec  ");
            strSql.Append(" FROM tbGoodsType ");

            strSql.AppendFormat(" where isnull(visible,0)=1 and (parentId = 0 and typeName ='{0}')"
                               , sMenuName);
            strSql.AppendFormat("  or (parentId in (select typeId from tbGoodsType where isnull(visible,0)=1 and (parentId = 0 and typeName ='{0}')) and typeId in (select gt.parentId from tbGoodsType gt join tbGoods g on gt.typeId = g.typeId and g.visible=1)) "
                                 , sMenuName);
                                

            return DbHelperSQL.Query(strSql.ToString());
        }


        public DataSet GetListByParentId(int iParentId,int iDepth)
        {
          StringBuilder sWhere = new StringBuilder();
            if (iDepth == 0)
            {
                sWhere.AppendFormat("  and g.typeid in (select typeId from tbGoodsType where parentId in (select typeId from tbGoodsType where parentId={0} ))", iParentId.ToString());

            }


            else if (iDepth == 1)
            {
                sWhere.AppendFormat(" and g.typeId in (select typeId from tbGoodsType where parentId={0} )", iParentId.ToString());

            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select typeId,parentId,typeName,typeOrder,typedepth,visible,typeLink,typeContent,case isnull(typePic,'') when '' then 'default.jpg' else typePic end as typePic,ishead,typeKey ");
         
            if (iDepth == 0)
            {
                strSql.Append(" from tbGoodsType where typeId in(select gt.parentId ");
            }
            else if (iDepth == 1)
            {
                strSql.Append(" from tbGoodsType where typeId in(select gt.typeId ");
            }
            strSql.Append(" FROM tbGoodsType gt join tbGoods g on gt.typeId = g.typeid and isnull(gt.visible,0)=1 and isnull(g.visible,0)=1 ");
            strSql.Append(sWhere.ToString());
            strSql.Append(") and isnull(visible,0)=1 ");
            strSql.Append(" order by typeOrder ");
  
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" typeId,parentId,typeName,typeOrder,typedepth,visible,typeLink,typeContent,typePic,ishead,typeKey ");
            strSql.Append(" FROM tbGoodsType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbGoodsType ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.typeId desc");
            }
            strSql.Append(")AS Row, T.*  from tbGoodsType T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbGoodsType";
            parameters[1].Value = "typeId";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

